package cn.kgc.shiro02.controller;

import cn.kgc.shiro02.entity.User;
import cn.kgc.shiro02.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author: mosin
 * @version: v1.0  2022/5/31
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login(String username,String password){

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(usernamePasswordToken);
        boolean authenticated = subject.isAuthenticated();
        System.out.println("认证通过： " + authenticated);
        return "redirect:/index.jsp";
    }


    @RequestMapping("logout")
    public String logout(){
        System.out.println("退出登录");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "/log.jsp";
    }


    @RequestMapping("reg")
    public String reg(User user){

        boolean b = userService.saveUser(user);
        if(b){
            return "redirect:/log.jsp";
        }
        return "redirect:/reg.jsp";

    }

}
